package com.qf.service.impl;

import com.qf.dao.UserDao;
import com.qf.entity.User;

import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2020-05-21 16:36:43
 */
@Service
public class UserServiceImpl implements UserService {
   @Autowired
    UserDao userDao;
    /**
     * 通过ID查询单条数据
     *
     * @param uid 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer uid) {
        Optional<User> byId = userDao.findById(uid);
        if (byId.isPresent()){

            return byId.get();
        }
        return null;
    }



    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {

        return  userDao.save(user);
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public String update(User user) {
        User user1 = userDao.saveAndFlush(user);
        if(user1!=null){
            return "success";
        }
        return "fail";
    }

    /**
     * 通过主键删除数据
     *
     * @param uid 主键
     * @return 是否成功
     */
    @Override
    public String deleteById(Integer uid) {
        try {
              userDao.deleteById(uid);
              return "success";
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }
}